package me.xplabs.ui.baseui 
{
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import me.xplabs.Globals;
	import me.xplabs.ui.BaseSprite;
	
	/**
	 * ...
	 * @author xiaohan
	 */
	public class Label extends BaseSprite 
	{
		private var _textField				:TextField;
		private var _border					:Panel;
		private var _fontSize				:int;
		private var _fontColor				:uint;
		private var _underLine				:Boolean;
		protected var _width				:int;
		protected var _height				:int;
		public function Label() 
		{
		}
		public function show(value:String, pw:int = 0, ph:int = 0):void
		{
			//this.mouseChildren= false;
			//this.mouseChildren = false;
			_textField = new TextField();
			_textField.textColor = 0xD3C832;
			_fontSize = 0xD3C832;
			_fontColor = 12;
			_textField.text = value;
			_textField.selectable = false;
			_width = pw;
			_height = ph;
			//if (pw > 0) _textField.width = pw;
			//else _textField.width = _textField.textWidth + 4;
			//if (ph > 0) _textField.height = ph;
			//else _textField.height = _textField.textHeight +4;
			setWH(pw, ph);
			//_textField.filters = [new GlowFilter(0x000000, 1, 1.15, 1.15, 10)];
			addChild(_textField);
		}
		public function border(type:int, pw:int, ph:int):void
		{
			if (!_border)
			{
				_border = Globals.ui.panel(type, pw, ph);
				addChildAt(_border, 0);
			}else _border.setWH(pw, ph);
			_textField.x = (_border.width -_textField.width) / 2;
			_textField.y = (_border.height -_textField.height) / 2;
		}
		public function get text():String 
		{
			return _textField.text;
		}
		public function set text(value:String):void
		{
			_textField.text = value;
			repairPos();
			middle();
		}
		
		public function get textField():TextField 
		{
			return _textField;
		}
		/**
		 * 加粗
		 */
		public function bold():void
		{
			var str:String = this.text;
			_textField.text = "";
			appendHtml("<b>" + str + "</b>");
		}
		public function set color(p_color:uint):void
		{
			//_textField.textColor = p_color;
			_fontColor = p_color;
			var t:TextFormat = _textField.defaultTextFormat;
			if (!t) t = new TextFormat(null, _fontSize, _fontColor);
			else t.color = _fontColor;
			_textField.defaultTextFormat = t;
			_textField.text = _textField.text;
		}
		public function set size(p_size:int):void
		{
			_fontSize = p_size;
			var t:TextFormat = _textField.defaultTextFormat;
			if (!t) t = new TextFormat(null, _fontSize, _fontColor);
			else t.size = _fontSize;
			_textField.defaultTextFormat = t;
			_textField.text = _textField.text;
		}
		public function set leading(p_leading:int):void
		{
			var t:TextFormat = _textField.defaultTextFormat;
			if (!t) t = new TextFormat(null, _fontSize, _fontColor);
			t.leading = p_leading;
			_textField.defaultTextFormat = t;
			_textField.text = _textField.text;
		}
		public function set underLine(value:Boolean):void
		{
			_underLine = value;
			var t:TextFormat = _textField.defaultTextFormat;
			if (!t) t = new TextFormat();
			t.underline = value;
			_textField.defaultTextFormat = t;
			_textField.text = _textField.text;
		}
		
		public function setColor(color:String, value:String = null):void {
			if (value == null) {
				var str:String = text;
				_textField.text = "";
				this.appendHtml("<font color='" + color + "'>" + str);
			}else
			{
				_textField.text = "";
				appendHtml("<font color='" + color + "'>" + value);
			}
			repairPos();
			middle();
		}	
		private function repairPos():void
		{
			if (_width == 0) _textField.width = _textField.textWidth + 4;
			if (_height == 0) _textField.height = _textField.textHeight + 4;
		}
		
		public function appendHtml(value:String):void
		{
			_textField.htmlText = _textField.htmlText + value;
			repairPos();
			middle();
		}
		public function middle():void
		{
			if (!_border) return;
			_textField.x = (_border.width -_textField.width) / 2;
			_textField.y = (_border.height -_textField.height) / 2;
		}
		
		public function setWH(pw:int = 0,ph:int=0):void 
		{
			if (pw > 0) 
			{
				_textField.width = pw;
				_textField.multiline = true;
				_textField.wordWrap = true;
			}
			else
			{
				_textField.width = _textField.textWidth + 4;
				_textField.multiline = false;
				_textField.wordWrap = false;
			}
			if (ph > 0) _textField.height = ph;
			else _textField.height = _textField.textHeight +4;
		}
		public function freshText():void
		{
			//立即更新_textField的属性解决方案,会触发Event.SCROLL事件 
			var  storeHeight:Number = _textField.height; 
			_textField.height = _textField.textHeight; 
			_textField.height = storeHeight;
		}
	}
}